﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data;
using System.Data.SqlClient;
using QLCONGNOLIBS.Securities;
using QLCONGNOLIBS.Models.EntityFW;

namespace QLCONGNOLIBS.Models
{
    public class CustomerModels : BaseModels<ICustomer>
    {             
        private const string GET_CUSTOMERS = "SP_GET_CUSTOMERS";
        private const string SP_IN_UP_CUSTOMERS = "SP_INSERT_UPDATE_CUSTOMER";

        public CustomerModels(SqlConnection objConnection, SecurityEntity objSecurity)
                : base(objConnection, objSecurity){ }

        protected internal override DataTable GetElements()
        {
            DataTable objTable;
            try
            {
                SqlParameter[] param = { };              
                objTable = ExecProcReturnTable(GET_CUSTOMERS, param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return objTable;            
        }

        protected internal override bool InsertUpdateElement(ICustomer obj, short shOpt)
        {
            try
            {                
                SqlParameter[] objParam = new SqlParameter[18];
                objParam[0] = CreateParammeter("@Flag", shOpt);
                objParam[1] = CreateParammeter("@CustomerID", obj.CustomerID);
                objParam[2] = CreateParammeter("@CustomerName", obj.CustomerName);
                objParam[3] = CreateParammeter("@CompanyName", obj.CompanyName);
                objParam[4] = CreateParammeter("@Address", obj.Address);
                objParam[5] = CreateParammeter("@PhoneNo", obj.PhoneNo);
                objParam[6] = CreateParammeter("@Email", obj.Email);
                objParam[7] = CreateParammeter("@CreditLine", obj.CreditLine);
                objParam[8] = CreateParammeter("@Due", obj.Due);
                objParam[9] = CreateParammeter("@Gov", obj.Gov);
                objParam[10] = CreateParammeter("@CityID", obj.CityID);
                objParam[11] = CreateParammeter("@Employee_ID", obj.Employee_ID);
                objParam[12] = CreateParammeter("@IsActive", obj.IsActive);
                objParam[13] = CreateParammeter("@IsDeleted", obj.IsDeleted);
                objParam[14] = CreateParammeter("@Fax", obj.Fax);
                objParam[15] = CreateParammeter("@BankAccount", obj.BankAccount);
                objParam[16] = CreateParammeter("@BankName", obj.BankName);
                objParam[17] = CreateParammeter("@Note", obj.Note);
                ExecProcNoReturn(SP_IN_UP_CUSTOMERS, objParam);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return true;            
        }        
    }
}
